package com.ganbo.ms.nio;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author ganbo
 * @date 2022/11/26 17:44
 */
public class Channel2 {

    public static void main(String[] args) throws IOException {

        File file = new File("c:/hello.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel channel = fileInputStream.getChannel();


        File file1 = new File("c:/hello2222222222222.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
        FileChannel channel1 = fileOutputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(5);


        while (true) {
            buffer.clear();
            int read = channel.read(buffer);
            System.out.println("read====>" + read);
            if (read != -1) {
                System.out.println("读取的内容===============<>" + new String(buffer.array()));
                buffer.flip();
                channel1.write(buffer);
            } else {
                break;
            }
        }

        channel.close();
        channel1.close();
    }
}
